#include "../_public.h"

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		printf("Using:./demo33 ip port\nExample:./demo33 192.168.199.128 5005\n\n");
		return -1;
	}

	CTcpClient TcpClient;

	// 向服务端发起连接请求
	if(TcpClient.ConnectToServer(argv[1], atoi(argv[2])) == false){
		printf("TcpClient.ConnectToServer(%s, %s) failed.\n", argv[1], argv[2]);
		return -1;
	}

	char buffer[102400];
	int ibuflen = 0;
	int cnt = 0;

	CLogFile logfile;
	if(logfile.Open("/tmp/demo33.log", "a+") == false){
		printf("logfile.Open() failed.\n");
	}


	// 与服务端通讯，发送一个报文后等待回复，然后再发下一个报文。
	for (int i = 0; i < 100000; ++i)
	{
		SPRINTF(buffer, sizeof(buffer),"这是第%d个女生，编号%03d", i+1, i+1);
		if ( TcpClient.Write(buffer) == false ) // 向服务端发送请求报文
		{
			// perror("send");
			break;
		}
		logfile.Write("发送：%s\n", buffer);

		// 接收服务端的回应报文
		while (true){
			memset(buffer, 0, sizeof(buffer));
			if ( TcpRead(TcpClient.m_connfd, buffer, &ibuflen, -1) == false){
				break;
			}
			logfile.Write("接收：%s\n", buffer);
			cnt++;
		}		
	}
	// 接收服务端的回应报文
	while (cnt < 100000){
		memset(buffer, 0, sizeof(buffer));
		if ( TcpRead(TcpClient.m_connfd, buffer, &ibuflen) == false){
			break;
		}
		logfile.Write("接收：%s\n", buffer);
		cnt++;
	}
	// 第4步：关闭socket，释放资源。
	// close(sockfd);
}
